using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using C4F.DevKit.Messaging;

namespace MapiApplication
{
    public partial class Credential : Form
    {
        private MapiManager manager;

        List<string> credentialList;

        public Credential()
        {
            InitializeComponent();
        }
        public Credential(ref MapiManager manager, ref List<string> credentialList)
        {
            InitializeComponent();
            this.manager = manager;

            if (credentialList == null)
                credentialList = new List<string>();
            this.credentialList = credentialList;

            if (this.credentialList != null)
            {
                if (credentialList.Count > 0)
                {
                    txtUsername.Text = credentialList[0];
                    txtPassword.Text = credentialList[1];
                    txtHost.Text = credentialList[2];
                    txtPort.Text = credentialList[3];
                }
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUsername.Text))
            {
                MessageBox.Show("Username can not be empty.");
                return;
            }
            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                MessageBox.Show("Password can not be empty.");
                return;
            }
            if (string.IsNullOrEmpty(txtHost.Text))
            {
                MessageBox.Show("Host can not be empty.");
                return;
            }
            if (string.IsNullOrEmpty(txtPort.Text))
            {
                MessageBox.Show("Port can not be empty.");
                return;
            }
            UInt32 portResult;
            
            if (! UInt32.TryParse(txtPort.Text, out portResult))
            {
                  
                MessageBox.Show("Port should be integer", "Credential", MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                return;
            }
            
            credentialList.Insert(0,txtUsername.Text);
            credentialList.Insert(1,txtPassword.Text);
            credentialList.Insert(2,txtHost.Text);
            credentialList.Insert(3,txtPort.Text);

            manager.CreateCredentialsToSendMail(txtUsername.Text, txtPassword.Text, txtHost.Text,(Int32) portResult);
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }     
    }
}